package com.sto.stoapp.activity.administrator;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.sto.stoapp.activity.R;
import com.sto.stoapp.broadcast.HomeBroadAction;
import com.sto.stoapp.db.StoFilePreference;
import com.sto.stoapp.listener.StoOnClickListener;
import com.sto.stoapp.util.Helper;
import com.sto.stoapp.widget.StoHomeActivity;

public class AdminServerIpActivity extends StoHomeActivity {

	private EditText ipEdit, ipsEdit, iprEdit;

	private Button ipSaveBtn, ipsSaveBtn, iprSaveBtn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.admin_server_ip);
		this.setTitleBottom(R.string.admin_server_ip,
				HomeBroadAction.ADMINISTRATOR_BROAD);
		initWidget();
		initListener();
	}

	private void initListener() {
		iprSaveBtn.setOnClickListener(new StoOnClickListener(this) {

			@Override
			public void mHonClick(View view) {
				String iprContent = iprEdit.getText().toString().trim();
				if (iprContent.equals("")) {
					Helper.showToast(AdminServerIpActivity.this,
							getText(R.string.input_not_null).toString());
				} else {
					StoFilePreference.getInstance(AdminServerIpActivity.this)
							.setServerIpr(iprContent);
					Helper.showToast(AdminServerIpActivity.this,
							getText(R.string.save_success).toString());
				}
			}
		});

		ipsSaveBtn.setOnClickListener(new StoOnClickListener(this) {

			@Override
			public void mHonClick(View view) {
				String ipsContent = ipsEdit.getText().toString().trim();
				if (ipsContent.equals("")) {
					Helper.showToast(AdminServerIpActivity.this,
							getText(R.string.input_not_null).toString());
				} else {
					StoFilePreference.getInstance(AdminServerIpActivity.this)
							.setServerIps(ipsContent);
					Helper.showToast(AdminServerIpActivity.this,
							getText(R.string.save_success).toString());
				}
			}
		});

		ipSaveBtn.setOnClickListener(new StoOnClickListener(this) {

			@Override
			public void mHonClick(View view) {
				String ipContent = ipEdit.getText().toString().trim();
				if (ipContent.equals("")) {
					Helper.showToast(AdminServerIpActivity.this,
							getText(R.string.input_not_null).toString());
				} else {
					StoFilePreference.getInstance(AdminServerIpActivity.this)
							.setServerIp(ipContent);
					Helper.showToast(AdminServerIpActivity.this,
							getText(R.string.save_success).toString());
				}
			}
		});


	}

	private void initWidget() {
		String serverIp = StoFilePreference.getInstance(this).getServerIp();
		String serverIps = StoFilePreference.getInstance(this).getServerIps();
		String serverIpr = StoFilePreference.getInstance(this).getServerIpr();
		ipEdit = (EditText) findViewById(R.id.ip_settings);
		ipEdit.setText(serverIp);
		ipEdit.setSelection(serverIp.length());

		ipsEdit = (EditText) findViewById(R.id.ips_settings);
		iprEdit = (EditText) findViewById(R.id.ipr_settings);


		ipsEdit.setText(serverIps);
		// ipsEdit.setSelection(serverIps.length());
		iprEdit.setText(serverIpr);
		ipSaveBtn = (Button) findViewById(R.id.ip_save);
		ipsSaveBtn = (Button) findViewById(R.id.ips_save);
		iprSaveBtn = (Button) findViewById(R.id.ipr_save);
	}

}
